﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Stats
{
    class GrafDny : IGraf
    {
        public override string text { get; set; }
        public override List<string> obdobi { get; set; }

        public GrafDny()
        {
            this.text = "Dny";
            obdobi = null;
            obdobi = DataLib.Settings.databases.dbPujcky.QueryGetListString("SELECT table_name from information_schema.tables WHERE table_name LIKE 'Vracene%'");
        }

        public override List<KeyValuePair<string, int>> GetDataKontext(string obdobiArg)
        {
            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            List<Trzba> values = NacisDny(obdobiArg);
            foreach (Trzba value in values)
                valueList.Add(new KeyValuePair<string, int>(value.Date.ToString("dd. MM."), value.Penize));
            return valueList;
        }

        /// <summary>
        /// Secte trzby ze vsech tabulek
        /// </summary>
        /// <returns></returns>
        private List<Trzba> NacisDny(string obdobi)
        {
            List<DateTime> tablesVracene = DataLib.Settings.databases.dbPujcky.QueryGetDateTimeleList("SELECT min(od) FROM Vracene" + obdobi.ToString() + " GROUP BY od");
            List<Trzba> trzby = new List<Trzba>();
            foreach (DateTime dateVracene in tablesVracene)
            {
                string date = dateVracene.ToString("yyyy-MM-dd");
                string dateAlfter = dateVracene.AddDays(1).ToString("yyyy-MM-dd");
                int trzba = DataLib.Settings.databases.dbPujcky.QueryGetInt("SELECT SUM(cena) FROM Vracene" + obdobi + " WHERE [od] > '" + date + "' AND [od] < '" + dateAlfter + "';");

                trzby.Add(new Trzba(DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture), trzba));
            }



            /*List<DateTime> tablesNevracene = DataLib.Settings.databases.dbPujcky.QueryGetDateTimeleList("SELECT min(od) FROM Nevracene" + obdobi.ToString() + " GROUP BY od");
            List<Trzba> trzbyNevracene = new List<Trzba>();
            foreach (DateTime dateNevracene in tablesNevracene)
            {
                string date = dateNevracene.ToString("yyyy-MM-dd");
                string dateAlfter = dateNevracene.AddDays(1).ToString("yyyy-MM-dd");
                int trzba = DataLib.Settings.databases.dbPujcky.QueryGetInt("SELECT SUM(cena) FROM Nevracene" + obdobi + " WHERE [od] > '" + date + "' AND [od] < '" + dateAlfter + "';");
                trzby.Add(new Trzba(DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture), trzba));
            }*/
            return trzby;
        }
    }
}
